package com.gc.ykqx.ui.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gc.ykqx.R;
import com.gc.ykqx.service.entity.Count;
import com.gc.ykqx.service.util.Constant;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

/**
 * Created by clem on 2017/3/22.
 */
public class CountListAdapter extends BaseAdapter {
    private Context mContext;
    private List<Count> countList;
    private LayoutInflater inflater;
    private OnCheckedChangeListener listener;
    private String siteType;//当水文站时隐藏 第七项
    private static final String TAG = "CountListAdapter";

    public CountListAdapter(Context mContext, List<Count> countList, String siteType) {
        this.mContext = mContext;
        this.countList = countList;
        this.inflater = LayoutInflater.from(mContext);
        this.siteType = siteType;
    }

    @Override
    public int getCount() {
        return countList.size();
    }

    @Override
    public Object getItem(int position) {
        return countList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Count count = countList.get(position);
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.lv_count_item, null, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Resources resources = mContext.getResources();
        if (count != null) {
            if (position == 0 || position == 6) {
                Drawable drawable = resources.getDrawable(R.drawable.count_left_item_bg);
                holder.layoutValue.setBackground(drawable);
                holder.tvValue.setTextColor(Color.WHITE);
            } else {
                Drawable drawable = resources.getDrawable(R.drawable.count_right_item_bg);
                holder.layoutValue.setBackground(drawable);
                holder.tvValue.setTextColor(Color.parseColor("#707070"));
            }
            if (siteType.equals(Constant.SiteType.SITE_SW) && position == 7) {
                holder.tvName.setVisibility(GONE);
                holder.tvValue.setVisibility(GONE);
            } else {
                holder.tvName.setVisibility(VISIBLE);
                holder.tvValue.setVisibility(VISIBLE);
            }
            holder.tvName.setText(count.getName());
            if (count != null) {
                if (count.getValue() == null) {
                    count.setValue("-999");
                } else {
                    if (count.getValue().equals("-999")) {
                        holder.tvValue.setText("-");
                    } else {
                        holder.tvValue.setText(count.getValue());
                    }
                }
            } else {
                Log.e(TAG, "getView: " + position);
            }
            if (count.isShow()) {
                holder.ckChoose.setVisibility(VISIBLE);
            } else {
                holder.ckChoose.setVisibility(GONE);
            }
            holder.ckChoose.setChecked(count.isChecked());
            holder.ckChoose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onChecked(position);
                    }
                }
            });
        }
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_value)
        TextView tvValue;
        @BindView(R.id.layout_value)
        RelativeLayout layoutValue;
        @BindView(R.id.ck_choose)
        CheckBox ckChoose;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface OnCheckedChangeListener {
        void onChecked(int position);
    }

    public void setListener(OnCheckedChangeListener listener) {
        this.listener = listener;
    }
}
